package com.vdcloud.mcclassroom.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.vdcloud.mcclassroom.entity.User;
import com.vdcloud.mcclassroom.service.IUserService;
import com.vdcloud.mcclassroom.vo.UserDetail;
import com.vdcloud.mcclassroom.vo.reqvo.LoginUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author songfazhun
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private IUserService userService;


    @Override
    public UserDetails loadUserByUsername(String str) throws UsernameNotFoundException {
        LoginUserVo loginUserVo = JSONUtil.toBean(str, LoginUserVo.class);
        User user = userService.getLoginUserByUserAccount(loginUserVo.getUserAccount());
        if (user == null) {
            return null;
        }
        UserDetail detail = new UserDetail();
        BeanUtil.copyProperties(user,detail);
        detail.setRemember(loginUserVo.getRemember());
        return detail;
    }
}
